<?php

  /**
   *
   */
    class Cicp_Graph_Menu
{
    private $_buttons     = array();
    private $_id          = '';

    const BUTTON_HL       = -1;
    const BUTTON_HL_RESET = -2;
    const BUTTON_DELETE   = -3;
    const BUTTON_HL_GROUP = -4;
    const BUTTON_LIVE     = -5;

    function __construct($id = null)
    {
        if ($id != null) {
            $this->setId($id);
        }
        else {
            $this->setRandomId();
        }
    }

    function __clone()
    {
        // set this id to some random one to avoid conflicts
        $this->setRandomId();
    }

    function setRandomId()
    {
        $this->setId('menu' . rand(10000, 99999));
    }

    function setId($id)
    {
        $this->_id = $id;
    }

    function __toString()
    {
        $menuString = 'menu,' . $this->getId() . "\n";
        foreach ($this->_buttons as $optionId => $buttonLabel)
        {
            if ($optionId != '-5') {
                // add button to string (<optionid>,<formatindex>,<label>)
                $menuString .= $optionId . ',2,' . $buttonLabel . "\n";
            }
            else {
                $menuString .= $optionId . ',-60,' . $buttonLabel . "\n";
            }
        }
        $menuString .= "endmenu\n";
        return $menuString;
    }

    function getButtonCount()
    {
        return count($this->_buttons);
    }

    function getId()
    {
        return $this->_id;
    }

    function addButton($buttonLabel, $index = null, $liveUpdateInterval = null)
    {
        if ($index == null) {
            $this->_buttons[] = $buttonLabel;
        }
        else {
            $this->_buttons[$index] = $buttonLabel;
        }
    }

    function getButtonLabelByOptionId($optionId)
    {
        $label = '';
        if (array_key_exists($optionId, $this->_buttons)) {
            $label = $this->_buttons[$optionId];
        }
        else {
            throw new Cicp_Exception_MenuOptionNotFound('Option with id ' . $optionId . ' does not exist in menu with identifier ' . $this->getId() . '.');
        }

        return $label;
    }
}